/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.device.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.app.device.domain.SettingBindInfo;
import me.zhengjie.modules.app.device.service.SettingBindInfoService;
import me.zhengjie.modules.app.device.service.dto.SettingBindInfoQueryCriteria;
import me.zhengjie.modules.app.hotel.service.HotelInfoService;
import me.zhengjie.modules.app.hotel.service.dto.HotelInfoDto;
import me.zhengjie.modules.app.hotelroom.service.HoteRoomInfoService;
import me.zhengjie.utils.SecurityUtils;

import org.apache.catalina.security.SecurityUtil;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.hutool.extra.spring.SpringUtil;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author Haojie Li
* @date 2021-03-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "应用：设备绑定配置信息")
@RequestMapping("/api/settingBindInfo")
public class SettingBindInfoController {

    private final SettingBindInfoService settingBindInfoService;
    private final HotelInfoService hotelInfoService;
    private final HoteRoomInfoService hoteRoomInfoService;

    @Log("导出数据")
    @ApiOperation("导出设备绑定配置信息")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('settingBindInfo:list')")
    public void download(HttpServletResponse response, SettingBindInfoQueryCriteria criteria) throws IOException {
    	
        settingBindInfoService.download(settingBindInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询设备绑定配置信息")
    @ApiOperation("查询设备绑定配置信息")
    @PreAuthorize("@el.check('settingBindInfo:list')")
    public ResponseEntity<Object> query(SettingBindInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(settingBindInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增设备绑定配置信息")
    @ApiOperation("新增设备绑定配置信息")
    @PreAuthorize("@el.check('settingBindInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody SettingBindInfo resources){
    	
    	HotelInfoDto hotelInfoDto = hotelInfoService.findByHotelNo(resources.getHotelNo());
    	if(hotelInfoDto == null) {
    		throw new BadRequestException("酒店编号不存在");
    	}

    	if(!hoteRoomInfoService.isExistByHotelNo(hotelInfoDto.getId(), resources.getHotelRoomNumber())) {
    		throw new BadRequestException("酒店房间编号不存在");
    	}
    	
    	if(settingBindInfoService.isRecordsExist(resources.getHotelNo(), resources.getHotelRoomNumber())) {
    		throw new BadRequestException("房间已经绑定");
    	}
    	
    	resources.setIsActive(1);//默认是激活状态
    	resources.setCreateBy(SecurityUtils.getCurrentUsername());//创建者是当前登录者
    	
    	return new ResponseEntity<>(settingBindInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改设备绑定配置信息")
    @ApiOperation("修改设备绑定配置信息")
    @PreAuthorize("@el.check('settingBindInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody SettingBindInfo resources){
    	
    	HotelInfoDto hotelInfoDto = hotelInfoService.findByHotelNo(resources.getHotelNo());
    	if(hotelInfoDto == null) {
    		throw new BadRequestException("酒店编号不存在");
    	}

    	if(!hoteRoomInfoService.isExistByHotelNo(hotelInfoDto.getId(), resources.getHotelRoomNumber())) {
    		throw new BadRequestException("酒店房间编号不存在");
    	}
    	
//    	if(settingBindInfoService.isRecordsExist(resources.getHotelNo(), resources.getHotelRoomNumber())) {
//    		throw new BadRequestException("房间已经绑定");
//    	}
    	
        settingBindInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除设备绑定配置信息")
    @ApiOperation("删除设备绑定配置信息")
    @PreAuthorize("@el.check('settingBindInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        settingBindInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}